package com.sp.scmdb.action;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.sp.scmdb.bean.LabelIOBean;
import com.sp.scmdb.bean.MonitorDataBean;
import com.sp.scmdb.bean.UserBean;
import com.sp.scmdb.common.action.BaseAction;
import com.sp.scmdb.entity.Employee;
import com.sp.scmdb.service.EmployeeService;
import com.sp.scmdb.service.LabelIOService;
import com.sp.scmdb.service.MonitorService;

@Controller("monitorDataAction")
@Scope("prototype")
public class MonitorDataAction extends BaseAction<MonitorDataBean> {

	private static final long serialVersionUID = 1L;
	private static Log log = LogFactory.getLog(MonitorDataAction.class);
	
	private MonitorService monitorService;
	private EmployeeService empService; 
	private LabelIOService labelIOService;

	private String searchType;
	private String idShift;
	private String department;
	private String workType;
	private String idArea;
	private String idReader;
	private String empType;

	public String findAll(){
		try{
			UserBean user = (UserBean)session.get("user");
			
			if ("1".equals(searchType)) {
				entity.setAllList(monitorService.allEmpShow(user, department, workType));
				entity.setList(entity.getAllList().subList(entity.getCurrentRow(), entity.getLastRow()));	
				
			} else if ("2".equals(searchType)) {
				entity.setAllList(monitorService.shiftsEmpShow(idShift, user, department, workType));
				entity.setList(entity.getAllList().subList(entity.getCurrentRow(), entity.getLastRow()));
				
			} else if ("3".equals(searchType)) {
				entity.setAllList(monitorService.readerEmpShow(idReader, department,empType, workType, user));
				entity.setList(entity.getAllList().subList(entity.getCurrentRow(), entity.getLastRow()));
				
			} else if ("4".equals(searchType)) {
				entity.setAllList(monitorService.areaEmpShow(idArea, department, workType, user));
				entity.setList(entity.getAllList().subList(entity.getCurrentRow(), entity.getLastRow()));
				
			} else if ("5".equals(searchType)) {
				entity.setAllList(monitorService.readerOutEmpShow(idReader, department, workType, user));
				entity.setList(entity.getAllList().subList(entity.getCurrentRow(), entity.getLastRow()));				
			}		
		} catch(RuntimeException e){			
			log.error("findAll fail!reason:" + e.getMessage());
			return "fail";
		} catch(Exception e){
			log.error("findAll fail!reason:" + e.getMessage());
			return "error";
		}
		return SUCCESS;
	}
	
	@SuppressWarnings("unchecked")
	public String findTruck(){
		LabelIOBean labelIOBean = new LabelIOBean();
		String empID = request.getParameter("empID");
		String inTime = request.getParameter("inTime");
		String outTime = request.getParameter("outTime");
		Employee emp = empService.getById(empID);
		labelIOBean.setEmpID(empID);
		labelIOBean.setDepID(emp.getDepart().getDeptName());
		if (emp.getDuty() != null) {
			labelIOBean.setDutyID(emp.getDuty().getName());
		}		
		labelIOBean.setInTime(inTime);
		labelIOBean.setLabelID(emp.getEmpLableID());
		labelIOBean.setEmpName(emp.getEmpName());
		if (StringUtils.isEmpty(outTime)){
			labelIOBean.setOutTime((new DateTime()).toString("yyyy-MM-dd HH:mm:ss"));
		} else{
			labelIOBean.setOutTime(outTime);
		}
		
		if (emp.getWorkType() != null) {
			labelIOBean.setWorkTypeID(emp.getWorkType().getName());	
		}
		
		try {
			
			labelIOBean = labelIOService.queryLabelIODetail(labelIOBean, new LabelIOBean());
			getEntity().setAllList(labelIOBean.getAllList());
			getEntity().setPageSize(50);
			session.put("labelIOBeanDetail", labelIOBean);
		} catch (Exception e) {
			log.error("get labelIO detail failed! reason:" + e.getMessage());
			return "fail";
		}
		
		return "truckSuccess";
	}

	
	public void setEmpService(EmployeeService empService) {
		this.empService = empService;
	}
	
	public void setMonitorService(MonitorService monitorService) {
		this.monitorService = monitorService;
	}

	public String getSearchType() {
		return searchType;
	}

	public void setSearchType(String searchType) {
		this.searchType = searchType;
	}


	public String getEmpType() {
		return empType;
	}

	public void setEmpType(String empType) {
		this.empType = empType;
	}

	public String getIdShift() {
		return idShift;
	}

	public void setIdShift(String idShift) {
		this.idShift = idShift;
	}

	public String getDepartment() {
		return department;
	}

	public void setDepartment(String department) {
		this.department = department;
	}

	public String getWorkType() {
		return workType;
	}

	public void setWorkType(String workType) {
		this.workType = workType;
	}

	public String getIdArea() {
		return idArea;
	}

	public void setIdArea(String idArea) {
		this.idArea = idArea;
	}

	public String getIdReader() {
		return idReader;
	}

	public void setIdReader(String idReader) {
		this.idReader = idReader;
	}

	@Autowired
	public void setLabelIOService(LabelIOService labelIOService) {
		this.labelIOService = labelIOService;
	}
}
